<%
//This would stream data file instead of HTML codes
if(session.getAttribute("hashedcode")!=null)
{
    String code=(String)session.getAttribute("hashedcode");
    session.removeAttribute("hashedcode");
    DownloadController control = new DownloadController();
    control.doFileDownload(code,request,response);
    return;
}
else
{
%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="misc.Configurations"%>
<%@page import="controller.DownloadController"%>
<%@page import="data.File"%>
<%@page import="data.AuthorizationSession"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="styles.css" />
<title><%=Configurations.title%>Download</title>
</head>
<body>
<h1>Download attempt</h1>
<%
if(request.getParameter("file")==null)
{
    response.sendRedirect("DownloadForm.jsp");
}
else
{
    String code=request.getParameter("file").trim();
    DownloadController control = new DownloadController();
    File this_file = control.retrieveFile(code);
    String username = control.getUsername(request);

    if(this_file==null)
    {
        %>
        <div class="error">The code you entered is invalid. Please double-check your code.</div>
        <%
    }
    else if(control.download_times_out(this_file.getFileID()))
    {
       %>
       <div class="error">The file has reached the maximum download times (<%=Configurations.MaxDownloadsPerFile %> times).</div>
       <%
    }
    else if(control.is_expired(code))
    {
        %>
        <div class="error">We're sorry but the file you attempted to download has been expired now.</div>
        <%
    }
    else if(control.deleted_file(code))
    {
        %>
        <div class="error">We're sorry but the file you attempted to download has been deleted by the uploader.</div>
        <%
    }
    else if(!control.is_one_file_a_time(username,request.getRemoteAddr()))
    {
       %>
       <div class="error">You can only download a file at one time! You are downloading the other file at this moment</div>
       <%
    }
    else if(this_file.isProtected()==true)
    {
        boolean authorized=false;
        if(request.getParameter("password")!=null)
        {
            String pwd=request.getParameter("password").trim();
            if(control.isAuthorized(pwd,code))
            {
                authorized = true;
                session.setAttribute("hashedcode",this_file.getHashedCode());
                response.sendRedirect("Download.jsp");
            }
            else
            {
                %>
                <div class="error">The password you entered is not correct!</div>
                <%  
            }
        }

        if(!authorized)
        {
            %>
            <div align="center">
                <form action="Download.jsp?file=<%= this_file.getHashedCode()%>" method="post">
                This file is protected with a password. Please enter it you are given.
                <table border="0">
                    <tr>
                        <td>File name</td><td><%= this_file.getName() %></td>
                    </tr>
                    <tr>
                        <td>Password</td><td><input type="password" name="password"/></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="right"><input type="submit" value="Download"/></td>
                    </tr>
                </table>
                </form>
            </div>
            <%
        }
    }
    else//OKAY!
    {
        session.setAttribute("hashedcode",this_file.getHashedCode());
        response.sendRedirect("Download.jsp");
    }
}
%>
<br/>
<div align="right">
    <form name="input" action="DownloadForm.jsp" >
       <input type="submit" value="Enter Download code"/>
    </form>
</div> 
<%
if(AuthorizationSession.checkAuthorizationToken(request,null,false))
{
%>
<br/>
<div align="right">
<form>
    <input type="button" value="Back to management" onclick="javascript:window.location = 'ManageFiles.jsp';"/>
</form>
</div>
<%
}
%>
</body>
</html>
<%}%>